"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数gms_utility.format_call_stack()返回函数调用栈
Create At   : 2023/12/21
Owner       : @zou_jialiang050
Description :
    1.标准格式输出调用栈（包含头部）
    2.标准格式输出调用栈（不含头部）
    3.逗号分割的调用堆栈显示（不含头部）
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_49'
        self.function01 = 'fun01_whale_49'
        self.function02 = 'fun02_whale_49'
        self.function03 = 'fun03_whale_49'
    
    def test_gms_utility(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')
        
        text = "---step1:标准格式输出调用栈（包含头部）;expect:成功---"
        self.logger.info(text)
        sql_cmd1_1 = f'''
        CREATE OR REPLACE FUNCTION {self.function01}() returns text as \$\$
            DECLARE
                stack text;
            BEGIN
                select * INTO stack from gms_utility.format_call_stack('o');
                return stack;
            END;
        \$\$ LANGUAGE plpgsql;'''
        msg1_1 = self.sh_primy.execut_db_sql(sql_cmd1_1, dbname=self.dbname)
        self.logger.info(msg1_1)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, msg1_1,
                      text + '执行失败')

        sql_cmd1_2 = f"select * from {self.function01}();"
        msg1_2 = self.sh_primy.execut_db_sql(sql_cmd1_2, dbname=self.dbname)
        self.logger.info(msg1_2)
        self.assertIn('function anonymous object', msg1_2, text + '执行失败')

        text = "---step2:标准格式输出调用栈（不包含头部）;expect:成功---"
        self.logger.info(text)
        sql_cmd2_1 = f'''
        CREATE OR REPLACE FUNCTION {self.function02}() returns text  as \$\$
            DECLARE
                stack text;
            BEGIN
                select * INTO stack from gms_utility.format_call_stack('p');
                return stack;
            END;
        \$\$ LANGUAGE plpgsql;'''
        msg2_1 = self.sh_primy.execut_db_sql(sql_cmd2_1, dbname=self.dbname)
        self.logger.info(msg2_1)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, msg2_1,
                      text + '执行失败')

        sql_cmd2_2 = f"select * from {self.function02}();"
        msg2_2 = self.sh_primy.execut_db_sql(sql_cmd2_2, dbname=self.dbname)
        self.logger.info(msg2_2)
        self.assertIn('function anonymous object', msg2_2, text + '执行失败')

        text = "---step3:逗号分割的调用堆栈显示（不含头部）;expect:成功---"
        self.logger.info(text)
        sql_cmd3_1 = f'''
        CREATE OR REPLACE FUNCTION {self.function03}() returns text as \$\$
            DECLARE
                stack text;
            BEGIN
                select * INTO stack from gms_utility.format_call_stack('s');
                return stack;
            END;
        \$\$ LANGUAGE plpgsql;'''
        msg3_1 = self.sh_primy.execut_db_sql(sql_cmd3_1, dbname=self.dbname)
        self.logger.info(msg3_1)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, msg3_1,
                      text + '执行失败')

        sql_cmd3_2 = f"select * from {self.function03}();"
        msg3_2 = self.sh_primy.execut_db_sql(sql_cmd3_2, dbname=self.dbname)
        self.logger.info(msg3_2)
        self.assertIn('anonymous object', msg3_2, text + '执行失败')
    
    def tearDown(self):
        text = '----清理环境----'
        self.logger.info(text)
        clean_cmd1 = f"drop function {self.function01};" \
            f"drop function {self.function02};" \
            f"drop function {self.function03};"
        cle_msg1 = self.sh_primy.execut_db_sql(clean_cmd1, dbname=self.dbname)
        self.logger.info(cle_msg1)
        self.assertTrue(cle_msg1.count(self.constant.DROP_FUNCTION_SUCCESS_MSG)
                        == 3, text + '执行失败')
        
        clean_cmd2 = f"drop database {self.dbname};"
        clean_msg2 = self.sh_primy.execut_db_sql(clean_cmd2)
        self.logger.info(clean_msg2)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_msg2,
                      text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
